﻿using System;
using System.Net;
using DemoPathfinding.Hotfix.Components;
using ETHotfix;
using ETModel;

namespace DemoPathfinding.Hotfix.Handlers {
    [MessageHandler(AppType.Gate)]
    public class C2G_LoginGateHandler : AMRpcHandler<C2G_LoginGate, G2C_LoginGate> {
        protected override void Run(Session session, C2G_LoginGate message, Action<G2C_LoginGate> reply) {
            //抽象方法是同步的,需要操作异步代码,从这里转一手
            RunAsync(session, message, reply).Coroutine();
        }
        protected async ETVoid RunAsync(Session session, C2G_LoginGate message, Action<G2C_LoginGate> reply) {
            G2C_LoginGate response = new G2C_LoginGate();
            try {
                if (!Game.Scene.GetComponent<GateLoginKeyComponent>().CheckGateLoginKey(message.UserAccount, message.GateLoginKey)) {
                    response.Error = ErrorCode.ERR_ConnectGateKeyError;
                    response.Message = "GateLoginKey验证失败!";
                    reply(response);
                    return;
                }

                //!!! 连接上Gate服务器,必须调用本方法完成玩家的初始化!
                var gatePlayer = session.GatePlayerLoginSuccess(null);

                response.PlayerId = gatePlayer.Id;

                reply(response);
            } catch (Exception e) {
                ReplyError(response, e, reply);
            }
        }
    }
}